<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="150px">
      <el-form-item label="单位名称" prop="compname">
        <el-input
            v-model="queryParams.compname"
            placeholder="请输入单位名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择年度" clearable style="width: 240px">
          <el-option
              v-for="dict in sys_year"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="科室" prop="leader">
        <el-input
            v-model="queryParams.leader"
            placeholder="请输入科室"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable style="width: 240px">
          <el-option
              v-for="dict in filling_check_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属区县" prop="department">
        <el-input
            v-model="queryParams.department"
            placeholder="请输入所属区县"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位类型" prop="mainType">
        <el-select v-model="queryParams.mainType" placeholder="请选择单位类型" clearable style="width: 240px">
          <el-option
              v-for="dict in unit_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="九张表状态" prop="nineTable">
        <el-input
            v-model="queryParams.nineTable"
            placeholder="请输入九张表导入状态"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自评报告是否上传" prop="zpbg">
        <el-select v-model="queryParams.zpbg" placeholder="请选择是否" clearable style="width: 240px">
          <el-option
              v-for="dict in yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['finance:unitInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:unitInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['finance:unitInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['finance:unitInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border="true" stripe="true" :data="unitInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年度" width="100" align="center" prop="year" />
      <el-table-column label="单位名称" width="400" align="center" prop="compname" />
      <el-table-column label="所属县市" width="100" align="center" prop="department" />
      <el-table-column label="科室" align="center" prop="leader" width="120"/>
      <el-table-column label="填报进度" align="center" prop="progress" width="300">
        <template v-slot="scope">
          <el-progress
              :percentage="parseFloat(scope.row.progress)"
              :stroke-width="8"
              color="#409EFF">
          </el-progress>
        </template>
      </el-table-column>
      <el-table-column label="单位类型" align="center" width="100">
        <template #default="scope">
          <dict-tag :options="unit_type" :value="scope.row.mainType" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" width="100">
        <template #default="scope">
          <el-switch
              v-model="scope.row.status"
              active-value="2"
              inactive-value="1"
              @click="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="是否上传绩效表" align="center" width="120" prop="sjjz" >
        <template #default="scope">
          <dict-tag :options="yes_no" :value="scope.row.sjjz" />
        </template>
      </el-table-column>
      <el-table-column label="是否导入九张表" align="center" width="150">
        <template #default="scope">
          <dict-tag :options="yes_no" :value="scope.row.nineTable" />
        </template>
      </el-table-column>
      <el-table-column label="是否上传自评报告" align="center" width="150" prop="zpbg" >
        <template #default="scope">
          <dict-tag :options="yes_no" :value="scope.row.zpbg" />
        </template>
      </el-table-column>
      <el-table-column label="报告下载时间" align="center" prop="downloadTime" width="180">
        <template #default="scope">
          {{ parseTime(scope.row.downloadTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="view" @click="editMain(scope.row)">查看</el-button>
          <el-button link type="primary" icon="memo" @click="showPref(scope.row)">整体绩效表</el-button>
          <el-button link type="primary" icon="download" @click="downZip(scope.row)">下载公开报告</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Unitinfo">
import { listUnitinfo, getUnitinfo, delUnitinfo, changeStatus } from "@/api/finance/unitInfo";

const { proxy } = getCurrentInstance();
const { sys_year, unit_type, filling_check_status, yes_no } = proxy.useDict('sys_year', 'unit_type', 'filling_check_status', 'yes_no');

const unitInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    guid: null,
    compname: null,
    compid: null,
    year: null,
    compnamesn: null,
    compsnid: null,
    yearupfileGuid: null,
    yearnowfileGuid: null,
    leader: null,
    compId: null,
    deptId: null,
    progress: null,
    status: null,
    enclosureName: null,
    department: null,
    details: null,
    sort: null,
    enable: null,
    isvalid: null,
    pjxms: null,
    sjjz: null,
    preDeptId: null,
    mainType: null,
    nineTable: null,
    zpbg: null,
    downloadTime: null,
    verifyStatus: null,
    nineResult: null,
    reportResult: null,
  },
  rules: {
    guid: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询填报单位主列表 */
function getList() {
  loading.value = true;
  listUnitinfo(queryParams.value).then(response => {
    unitInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    guid: null,
    compname: null,
    compid: null,
    year: null,
    compnamesn: null,
    compsnid: null,
    yearupfileGuid: null,
    yearnowfileGuid: null,
    leader: null,
    compId: null,
    deptId: null,
    progress: null,
    status: null,
    enclosureName: null,
    department: null,
    remark: null,
    details: null,
    sort: null,
    createTime: null,
    updateTime: null,
    enable: null,
    isvalid: null,
    pjxms: null,
    sjjz: null,
    preDeptId: null,
    mainType: null,
    nineTable: null,
    zpbg: null,
    downloadTime: null,
    verifyStatus: null,
    nineResult: null,
    reportResult: null,
  };
  proxy.resetForm("unitInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加填报单位主";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getUnitinfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改填报单位主";
  });
}

/** 审核状态修改 */
function handleStatusChange(row) {
  let text = row.status === "2" ? "审核" : "取消审核";
  changeStatus(row.id, row.status);
  proxy.$modal.msgSuccess(text + "成功");
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除填报单位主编号为"' + _ids + '"的数据项？').then(function() {
    return delUnitinfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('finance/unitInfo/export', {
    ...queryParams.value
  }, `unitInfo_${new Date().getTime()}.xlsx`);
}

/** 是否渲染字段 */
function renderField(insert, edit) {
  return form.value.id == null ? insert : edit;
}

getList();
</script>